//
//  Ext+Calendar.swift
//  Schedule
//
//  Created by zhaojunming on 2024/10/24.
//

import UIKit

extension Calendar {
    // 生产日期
    func generateDates(for dateInterval: DateInterval, matching components: DateComponents) -> [Date] {
        var dates = [dateInterval.start]
        self.enumerateDates(startingAfter: dateInterval.start, matching: components, matchingPolicy: .nextTime) { result, _, stop in
            guard let date = result else { return }
            guard date < dateInterval.end else {
                stop = true
                return
            }
            dates.append(date)
        }
        return dates
    }

    func generateDays(for dateInterval: DateInterval) -> [Date] {
        generateDates(for: dateInterval, matching: dateComponents([.hour, .minute, .second], from: dateInterval.start))
    }
    
    // 获取指定日期的周末的范围
    func dateOfWeekend(containing date: Date) -> [Date] {
        guard let dateInterval = self.dateIntervalOfWeekend(containing: date) else {
            return []
        }
        return generateDates(for: dateInterval, matching: dateComponents([.hour, .minute, .second], from: dateInterval.start))
    }
}

